package wo.bsys.generator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import wo.common.util.WoJavaDocUtil;
import wo.common.util.WoUtil;
import wo.jpa.reader.entity.WoJPAEntity;
import wo.jpa.reader.util.JPAReaderUtil;

/**
 * wo-sbadmin2-sss项目代码生成器.
 * 
 * @author cailei
 * @date 2018年11月13日
 */
public class BSysGenerator {
	private final static Logger LOG = LogManager.getLogger(BSysGenerator.class);

	/**
	 * @param projectDir
	 * @param entityClass
	 */
	public static void createCode(String projectDir, String entityClass) {
		LOG.info("createCode:rootDir-" + projectDir + ",entityClass-" + entityClass);
		String srcDir = getSrcDir(projectDir);
		WoJavaDocUtil.setSrcPath(srcDir);
		// 构造实体数据
		WoJPAEntity e = new WoJPAEntity(entityClass);
		// 创建*Dto类
		JPAReaderUtil.createCode(srcDir, e, "dto", true);
		// 创建*Repository接口
		JPAReaderUtil.createCode(srcDir, e, "repository", false);
		// 创建*Service接口
		JPAReaderUtil.createCode(srcDir, e, "service", false);
		// 创建*ServiceImpl类
		JPAReaderUtil.createCode(srcDir, e, "service.impl", false);
		// 创建*Controller类
		JPAReaderUtil.createCode(srcDir, e, "controller", false);
	}

	private static String getSrcDir(String projectDir) {
		return WoUtil.getDirEndWithFileSeparator(projectDir) + "src/main/java/";
	}

	private static void createJsp(String fileDir, WoJPAEntity e, String jspName) {
		String filePath = fileDir + jspName;
		JPAReaderUtil.createFile(e, "entity-" + jspName, filePath);
	}

	/**
	 * @param projectDir
	 * @param entityClass
	 */
	public static void createJsp(String projectDir, String entityClass) {
		LOG.info("createCode:rootDir-" + projectDir + ",entityClass-" + entityClass);
		projectDir = WoUtil.getDirEndWithFileSeparator(projectDir);
		String srcDir = getSrcDir(projectDir);
		// 设置java源代码目录
		WoJavaDocUtil.setSrcPath(srcDir);
		// 构造实体数据
		WoJPAEntity e = new WoJPAEntity(entityClass);
		String fileDir = projectDir + "src/main/webapp/WEB-INF/page/" + e.getEntityNameLowerFirstChar() + "/";
		// 创建main.jsp代码
		createJsp(fileDir, e, "main.jsp");
		// 创建create.jsp代码
		createJsp(fileDir, e, "create.jsp");
		// 创建update.jsp代码
		createJsp(fileDir, e, "update.jsp");
		// 创建selector.jsp代码
		createJsp(fileDir, e, "selector.jsp");
	}
	
	/**
	 * @param projectDir
	 * @param entityClass
	 */
	public static void createInitializer(String projectDir, String[] pkgs) {
		LOG.info("createCode:rootDir-" + projectDir + ", pkgs-" + pkgs);
		projectDir = WoUtil.getDirEndWithFileSeparator(projectDir);
		String srcDir = getSrcDir(projectDir);
		// 设置java源代码目录
		WoJavaDocUtil.setSrcPath(srcDir);
		Map<String, List<WoJPAEntity>> modules = new HashMap<String, List<WoJPAEntity>>();
		for (String pkg : pkgs) {
			JPAReaderUtil.findEntitiesByPackage(projectDir, pkg, (projDir, entityCls) -> {
				// 构造实体数据
				WoJPAEntity e = new WoJPAEntity(entityCls);
				List<WoJPAEntity> entities = modules.get(e.getModule());
				if (entities == null) {
					entities = new ArrayList<WoJPAEntity>();
					modules.put(e.getModule(), entities);
				}
				entities.add(e);
			});
		}
		LOG.info("modules:" + modules);
		JPAReaderUtil.createFileByModule(modules, "BSysInitializer.java", projectDir + "src/test/java/wo/bsys/test/BSysInitializer.java");
	}

	public static void main(String[] args) {
		JPAReaderUtil.findEntitiesByPackage("../wo-sbadmin2-sss/", "wo.resume.po", (projectDir, entityCls) -> {
			createCode(projectDir, entityCls);
			createJsp(projectDir, entityCls);
		});
		createInitializer("../wo-sbadmin2-sss/", new String[]{"wo.department.po", "wo.resume.po"});
	}
}
